﻿$(function () {
    $("#btnData").click(function () {
        art.dialog.open('/DeviceData/Index', {
            title: '生产线数据',
            width: '100%',
            height: '100%',
            fixed: false,
            maxmin: true,
            shadeClose: false,
            resize: false,
            drag: false
        }, true)
    });

    doSearch();

    $("#btnSearch").click(function () {
        searchTimeAnalysisQuery();
    });
})

function doSearch() {
    //timeAnalysisQuery();
    targetAnalysis();
    searchTimeAnalysisQuery();
}

//时间分析
function timeAnalysisQuery() {

    Topro.Util.Ajax({
        type: "POST",
        url: "/LargeScreen/TimeAnalysisQuery",
        data: null,
        success: function (res) {

            if (res.success) {

                var chart = {
                    backgroundColor: '#000000',
                    type: 'bar'
                };

                var title = {
                    text: ''
                };

                var credits = {
                    enabled: false
                };

                var exporting = {
                    enabled: false
                };

                var xAxis = {
                    categories: ['301'],
                    title: {
                        text: null
                    }
                };

                var yAxis = {
                    min: 0,
                    max: 100,
                    title: {
                        text: '时间轴百分比',
                        align: 'high'
                    },
                    labels: {
                        overflow: 'justify'
                    }
                };

                var tooltip = {
                    valueSuffix: '%'
                };

                var plotOptions = {
                    bar: {
                        dataLabels: {
                            enabled: true
                        }
                    },
                    series: {
                        stacking: 'normal'
                    }
                };

                var legend = {
                    layout: 'horizontal',
                    align: 'right',
                    verticalAlign: 'top',
                    x: 0,
                    y: 50,
                    floating: true,
                    borderWidth: 1,
                    backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
                    shadow: true
                };

                var series = res.data.timeAnalysisSeries;

                var json = {};
                json.chart = chart;
                json.title = title;
                json.credits = credits;
                json.exporting = exporting;
                json.tooltip = tooltip;
                json.xAxis = xAxis;
                json.yAxis = yAxis;
                json.series = series;
                json.plotOptions = plotOptions;
                json.legend = legend;

                $('#time').highcharts(json);
            }
        }
    })

}

//目标分析
function targetAnalysis() {

    Topro.Util.Ajax({
        type: "POST",
        url: "/LargeScreen/TargetAnalysis",
        data: { lineId: 20 },
        success: function (res) {

            if (res.success) {
                // Build the chart
                Highcharts.chart('target', {
                    chart: {
                        backgroundColor: '#000000',
                        plotBackgroundColor: null,
                        plotBorderWidth: null,
                        plotShadow: false,
                        type: 'pie'
                    },
                    title: {
                        text: 'Output Chart'
                    },
                    credits: {  //去掉highcharts.com
                        enabled: false
                    },
                    exporting: {  //导出，不显示false
                        enabled: false,
                    },
                    tooltip: {
                        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                    },
                    plotOptions: {
                        pie: {
                            allowPointSelect: true,
                            cursor: 'pointer',
                            dataLabels: {
                                enabled: false
                            },
                            showInLegend: true
                        }
                    },
                    series: [{
                        name: 'Brands',
                        colorByPoint: true,
                        data: [{
                            name: '计划',
                            color: '#91CC75',
                            y: res.data.targetY
                        }, {
                            name: '实际',
                            color: '#5470C6',
                            y: res.data.realityY
                        }]
                    }]
                });
            }

        }
    })

}

//时间分析
function searchTimeAnalysisQuery() {

    var data = $("#formSearch").serialize();

    Topro.Util.Ajax({
        type: "POST",
        url: "/OEE/TimeAnalysisQuery",
        data: data,
        success: function (res) {

            if (res.success) {
                var htmls = doT.template($("#tpl").text());
                $("#data").html(htmls(res.data.dayList));

                var chart = {
                    backgroundColor: '#000000',
                    type: 'bar'
                };

                var title = {
                    text: ''
                };

                var credits = {
                    enabled: false
                };

                var exporting = {
                    enabled: false
                };

                var xAxis = {
                    categories: res.data.categories,
                    title: {
                        text: null
                    }
                };

                var yAxis = {
                    min: 0,
                    max: 100,
                    tickInterval:1,
                    title: {
                        text: '时间轴',
                        align: 'high'
                    },
                    labels: {
                        overflow: 'justify'
                    }
                };

                var tooltip = {
                    valueSuffix: '%'
                };

                var plotOptions = {
                    bar: {
                        dataLabels: {
                            enabled: true
                        }
                    },
                    series: {
                        stacking: 'normal'
                    }
                };

                var legend = {
                    backgroundColor: '#000000',
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: 0,
                    y: 10,
                    floating: true,
                    borderWidth: 1,
                    backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
                    shadow: true
                };

                var series = res.data.timeAnalysisSeries;


                var json = {};
                json.chart = chart;
                json.title = title;
                json.credits = credits;
                json.exporting = exporting;
                json.tooltip = tooltip;
                json.xAxis = xAxis;
                json.yAxis = yAxis;
                json.series = series;
                json.plotOptions = plotOptions;
                json.legend = legend;

                $('#time').highcharts(json);
            }
        }
    })

}